package com.inventive.jmscache.server;

import javax.jms.MessageProducer;
import javax.jms.JMSException;
import javax.jms.Destination;
import javax.jms.Message;

/**
 * @author Grant Little <grant.little@coreservices.com.au>
 */
public class JmsCacheMessageProducer implements MessageProducer {

    protected JmsCacheDestination destination;
    protected int deliveryMode;
    protected int priority;
    protected long timeToLive = -1L;
    protected boolean closed = false;
    protected boolean disableMessageTimestamp=false;
    protected boolean disableMessageID=false;
    protected JmsCacheSession session;

    public JmsCacheMessageProducer(JmsCacheSession session, JmsCacheDestination destination) {
        this.session = session;
        this.destination = destination;
    }

    public void setDisableMessageID(boolean disableMessageID) throws JMSException {
        this.disableMessageID = disableMessageID;
    }

    public boolean getDisableMessageID() throws JMSException {
        return disableMessageID;
    }

    public void setDisableMessageTimestamp(boolean disableMessageTimestamp) throws JMSException {
        this.disableMessageTimestamp = disableMessageTimestamp;
    }

    public boolean getDisableMessageTimestamp() throws JMSException {
        return disableMessageTimestamp; 
    }

    public void setDeliveryMode(int deliveryMode) throws JMSException {
        this.deliveryMode = deliveryMode;
    }

    public int getDeliveryMode() throws JMSException {
        return deliveryMode;
    }

    public void setPriority(int priority) throws JMSException {
        this.priority = priority;
    }

    public int getPriority() throws JMSException {
        return priority;
    }

    public void setTimeToLive(long timeToLive) throws JMSException {
        this.timeToLive = timeToLive;
    }

    public long getTimeToLive() throws JMSException {
        return timeToLive;
    }

    public Destination getDestination() throws JMSException {
        return destination;
    }

    public void close() throws JMSException {
        closed = true;
    }

    public void send(Message message) throws JMSException {
        if (null == destination) {
            throw new IllegalStateException("Destination is null");
        }
        if (session.isTransacted()) {
            session.startTransaction();
        }
        destination.addMessage((JmsCacheMessage)message);
        if (session.isTransacted()) {
            session.commit();
        }
    }

    public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
        if (!(message instanceof JmsCacheMessage)) {
            throw new IllegalArgumentException("Message is not an instance of JmsCacheMessage");
        }
        JmsCacheMessage jmsCacheMessage = (JmsCacheMessage)message;
        jmsCacheMessage.setJMSPriority(priority);
        jmsCacheMessage.setJMSDeliveryMode(deliveryMode);
        //TODO Set the timeToLive on the message
        destination.addMessage(jmsCacheMessage);
    }


    public void send(Destination destination, Message message) throws JMSException {
        if (null == destination || destination instanceof JmsCacheDestination) {
            throw new IllegalArgumentException("Supplied destination is either null or not an instance of a CachedJmsDestination");
        }
        @SuppressWarnings({"ConstantConditions"})
        JmsCacheDestination jmsDestination = (JmsCacheDestination)destination;
        jmsDestination.addMessage((JmsCacheMessage)message);
    }

    public void send(Destination destination, Message message, int i, int i1, long l) throws JMSException {
        if (null == destination || destination instanceof JmsCacheDestination) {
            throw new IllegalArgumentException("Supplied destination is either null or not an instance of a CachedJmsDestination");
        }
        @SuppressWarnings({"ConstantConditions"})
        JmsCacheDestination jmsDestination = (JmsCacheDestination)destination;
        jmsDestination.addMessage((JmsCacheMessage)message);

    }


}
